package fr.toto.triseries;

import java.util.Date;
import java.util.NavigableSet;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;

import com.kokotchy.betaSeriesAPI.model.Episode;

import fr.toto.comparators.EpisodeComparator;

/**
 * 
 */

/**
 * @author tshepo
 * 
 */
public class EpisodesSet {

	private static ConcurrentSkipListSet<Episode> s = new ConcurrentSkipListSet<Episode>(
			new EpisodeComparator());

	public static synchronized void add(Episode ep) {
		if (ep != null)
			s.add(ep);
	}

	public static synchronized void clear() {
		s.clear();
	}

	public static synchronized void addAll(Set<Episode> ep) {
		for (Episode episode : ep)
			if (!episode.isSeen() && episode.getDate() != 0)
				s.add(episode);
	}

	public static synchronized void reset(Set<Episode> ep) {
		if (ep != null) {
			long date = (new Date()).getTime();
			int d = (int) (date / 1000);
			s.clear();
			for (Episode episode : ep)
				if (!episode.isSeen() && episode.getDate() != 0) {
					if (episode.getDate() < d)
						s.add(episode);
				}
		}
	}

	public static synchronized Episode find(Episode e) {
		return s.tailSet(e, true).floor(e);

	}

	public static synchronized Episode find(String url, String season,
			String episode) {
		Episode ep = new Episode();
		ep.setShowUrl(url);
		ep.setSeason(Integer.parseInt(season));
		ep.setEpisode(Integer.parseInt(episode));
		return find(ep);
	}

	public static synchronized Episode find(String url, int season, int episode) {
		Episode ep = new Episode();
		ep.setShowUrl(url);
		ep.setSeason(season);
		ep.setEpisode(episode);
		return find(ep);
	}

	public static synchronized Object[] ListATraiter() {
		return s.toArray();
	}

	public static synchronized NavigableSet<Episode> previousEpisodes(Episode ep) {
		Episode deb = new Episode();
		deb.setSeason(0);
		deb.setEpisode(0);
		deb.setShowUrl(ep.getShowUrl());
		return s.subSet(deb, true, ep, true);
	}

	public static synchronized NavigableSet<Episode> ShowList(Episode ep) {
		Episode deb = new Episode();
		deb.setSeason(-1);
		deb.setEpisode(-1);
		deb.setShowUrl(ep.getShowUrl());
		Episode fin = new Episode();
		fin.setSeason(10000);
		fin.setEpisode(10000);
		fin.setShowUrl(ep.getShowUrl());
		return s.subSet(deb, fin);
	}

}
